$(document).ready(function() {
	var products;
	
	$.ajax({
		  url: 'http://egestao/product/buscartodos',
		  dataType: 'json',
		  success: function(ret){
			  products = ret;
			  $('#grid').datagrid({
					title:'Editable DataGrid',
					iconCls:'icon-edit',
					width:660,
					height:250,
					singleSelect:true,
					idField:'id',
					url:'http://egestao/item/buscartodos',
					columns:[[
						{field:'id',title:'Item ID',width:60},
						{field:'productid',title:'Product',width:100,
							formatter:function(value){
								for(var i=0; i<products.length; i++){
									if (products[i].productid == value) return products[i].name;
								}
								return value;
							},
							editor:{
								type:'combobox',
								options:{
									valueField:'productid',
									textField:'name',
									data:products,
									required:true
								}
							}
						},
						{field:'listprice',title:'List Price',width:80,align:'right',editor:{type:'numberbox',options:{precision:1}}},
						{field:'unitcost',title:'Unit Cost',width:80,align:'right',editor:'numberbox'},
						{field:'attr1',title:'Attribute',width:150,editor:'text'},
						{field:'status',title:'Status',width:50,align:'center',
							editor:{
								type:'checkbox',
								options:{
									on: 'P',
									off: ''
								}
							}
						},
						{field:'action',title:'Action',width:70,align:'center',
							formatter:function(value,row,index){
								if (row.editing){
									var s = '<a href="#" onclick="saverow('+index+')">Save</a> ';
									var c = '<a href="#" onclick="cancelrow('+index+')">Cancel</a>';
									return s+c;
								} else {
									var e = '<a href="#" onclick="editrow('+index+')">Edit</a> ';
									var d = '<a href="#" onclick="deleterow('+index+')">Delete</a>';
									return e+d;
								}
							}
						}
					]],
					onBeforeEdit:function(index,row){
						row.editing = true;
						updateActions();
					},
					onAfterEdit:function(index,row){
						row.editing = false;
						updateActions();
					},
					onCancelEdit:function(index,row){
						row.editing = false;
						updateActions();
					}
				});
		  }
		});
});

function updateActions(){
	var rowcount = $('#grid').datagrid('getRows').length;
	for(var i=0; i<rowcount; i++){
		$('#grid').datagrid('updateRow',{
			index:i,
			row:{action:''}
		});
	}
}
function editrow(index){
	$('#grid').datagrid('beginEdit', index);
}
function deleterow(index){
	$.messager.confirm('Confirm','Are you sure?',function(r){
		if (r){
			dados = $('#grid').datagrid('getSelected');
			
			var data = {};
			data.id = dados.id;
			
			service('item', 'deletar', data);
			
			$('#grid').datagrid('deleteRow', index);
			updateActions();
		}
	});
}
function saverow(index){
	$('#grid').datagrid('endEdit', index);
	dados = $('#grid').datagrid('getSelected');
	var data = {};
	data.id 		= dados.id;
	data.productid 	= dados.productid;
	data.listprice 	= dados.listprice;
	data.unitcost 	= dados.unitcost;
	data.attr1 		= dados.attr1;
	data.status 	= dados.status;
	
	service('item', 'salvarOuAtualizar', data);
}
function cancelrow(index){
	$('#grid').datagrid('cancelEdit', index);
}
function insert(){
	var row = $('#grid').datagrid('getSelected');
	if (row){
		var index = $('#grid').datagrid('getRowIndex', row);
	} else {
		index = 0;
	}
	$('#grid').datagrid('insertRow', {
		index: index,
		row:{
			status:'P'
		}
	});
	$('#grid').datagrid('selectRow',index);
	$('#grid').datagrid('beginEdit',index);
}

function service(classe, metodo, data){
	$.ajax({
		  url: '/'+classe+'/'+metodo,
		  dataType: 'json',
		  type: "POST",
		  data: data,
		  success: function(ret){
			  console.log(ret);
		  }
	});
}
		